package com.lhy.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.lhy.domain.Result;
import com.lhy.domain.Student;
import com.lhy.service.IUserService;

@Controller
@RequestMapping("/register")
public class RegisterController {
	@Autowired
	private IUserService userService;
	
	@RequestMapping("")
	public String toIndex(HttpServletRequest request, Model model){
		return "register";
	}
	
	@RequestMapping("/do")
	public @ResponseBody
	Map<String, Object> login(String userName, String password, String nickName, String email, String phone, String sex,String img)  {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		Result result=new Result();
		result.setCode("1");
		result.setDes("成功");
		try{
			Student user =new Student();
			user.setEmail(email);
			user.setNickname(nickName);
			user.setPassword(password);
			user.setPhonenumber(phone);
			user.setSex(sex);
			user.setUsername(userName);
			Integer userId =(int)((Math.random()*9+1)*100000);
			user.setUserid(userId);
			user.setImg(img);
			userService.insert(user);
			Student u =userService.selectByUserNameANDUserpwd(userName, password);
			if(StringUtils.isEmpty(u)){
				System.out.println("用户名或密码输入错误！");
			}
			returnMap.put("userid", userId);
		}catch(Exception e){
				result.setCode("0");
				result.setDes(e.getMessage());
			
		}
		returnMap.put("result", result);
		return returnMap;
	}
}
